<template>
  <div>
    <div>
      <h1>VIP规则管理</h1>
    </div>
    <el-form :inline="true">
      <el-row>
        <el-col :span="6">
          <el-form-item label="平台：">
            <el-select
              v-model="selectData.roleVip"
              @change="handleStatusChange"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openAddDialogHandler"
            >新增</el-button
          >
          <el-button type="primary" @click="getVipRuleHandler">查询</el-button>
          <el-button type="primary" @click="resetHandler">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%" v-loading="dataLoading" element-loading-text="拼命加载中">
      <el-table-column prop="roleVip" label="平台" align="center" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.roleVip == 0">用户</span>
          <span v-else-if="scope.row.roleVip == 1">企业</span>
        </template>
      </el-table-column>
      <el-table-column prop="vipDesc" label="功能" align="center">
      </el-table-column>
      <el-table-column prop="amount" label="价格(元/月)" align="center">
      </el-table-column>
      <el-table-column prop="operate" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="openUpdateDialog(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteVipRuleHandler(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="selectData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="selectData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增弹窗 -->
    <el-dialog
      title="新增vip规则"
      width="30%"
      :visible.sync="insertDialogVisible"
    >
      <el-form>
        <el-form-item label="平台：">
          <el-select v-model="insertData.roleVip">
            <el-option
              v-for="item in addOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="功能">
          <el-input v-model="insertData.vipDesc"></el-input>
        </el-form-item>
        <el-form-item label="价格(元/月)">
          <el-input v-model="insertData.amount"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addVipRuleHandler">提交</el-button>
        <el-button @click="cancelAddRuleHanlder">取消</el-button>
      </div>
    </el-dialog>

    <!-- 修改弹窗 -->
    <el-dialog
      title="修改vip规则"
      width="30%"
      :visible.sync="updateDialogVisible"
    >
      <el-form>
        <el-form-item label="平台：">
          <div>
            <span v-if="updateData.roleVip === 0">用户</span>
            <span v-else-if="updateData.roleVip === 1">企业</span>
          </div>
        </el-form-item>
        <el-form-item label="功能">
          <el-input v-model="updateData.vipDesc"></el-input>
          <!-- <tinymce-editor
            v-model="updateData.vipDesc"
            :disabled="false"
          ></tinymce-editor> -->
        </el-form-item>
        <el-form-item label="价格(元/月)">
          <el-input v-model="updateData.amount"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateVipRuleHandler">提交</el-button>
        <el-button @click="cancelUpdateRuleHanlder">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getVipRule,
  updateVipRule,
  deleteVipRule,
  addVipRule,
} from "@/api/vip";
import TinymceEditor from "@/components/TinymceEditor.vue";
export default {
  components: { TinymceEditor },
  data() {
    return {
      //加载状态
      dataLoading:false,
      roleVip: {
        0: "用户",
        1: "企业",
      },
      options: [
        { value: "", label: "全部" },
        { value: 0, label: "用户" },
        { value: 1, label: "企业" },
      ],
      addOptions: [
        { value: 0, label: "用户" },
        { value: 1, label: "企业" },
      ],
      selectData: {
        roleVip: "",
        currentPage: 1, //当前页
        pageSize: 5, //每页大小
      },
      insertData: {
        roleVip: "",
        vipDesc: "",
        amount: "",
      },
      updateData: {
        vipId: "",
        roleVip: "",
        vipDesc: "",
        amount: "",
      },
      total: 0,
      tableData: [],
      insertDialogVisible: false,
      updateDialogVisible: false,
    };
  },
  methods: {
    //删除vip规则
    deleteVipRuleHandler(row) {
      this.$confirm(`此操作将删除该vip规则, 是否继续?`, "温馨提示", {
        confirmButtonText: "确定删除",
        cancelButtonText: "取消删除",
      })
        .then(() => {
          deleteVipRule(row.vipId).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getVipRuleHandler();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {});
    },

    //取消修改vip规则按钮
    cancelUpdateRuleHanlder() {
      this.updateDialogVisible = false;
    },
    //提交修改vip规则
    updateVipRuleHandler() {
      this.$confirm(
        `此操作将修改${
          this.roleVip[this.updateData.roleVip]
        }的vip规则, 是否继续?`,
        "温馨提示",
        {
          confirmButtonText: "确定修改",
          cancelButtonText: "取消修改",
        }
      )
        .then(() => {
          updateVipRule(this.updateData).then((res) => {
            this.updateDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getVipRuleHandler();
            }
          });
        })
        .catch(() => {});
    },
    //打开修改弹窗的方法
    openUpdateDialog(row) {
      this.updateDialogVisible = true;
      //使用$nextTick确保富文本正确加载数据
      this.$nextTick(() => {
        this.updateData.vipId = row.vipId;
        this.updateData.roleVip = row.roleVip;
        this.updateData.vipDesc = row.vipDesc || ""; // 确保内容正确传递
        this.updateData.amount = row.amount;
      });
    },
    //重置
    resetHandler() {
      this.selectData.roleVip = null;
      this.getVipRuleHandler();
    },
    //提交新增vip规则
    addVipRuleHandler() {
      this.$confirm(
        `此操作将新增${
          this.roleVip[this.selectData.roleVip]
        }的vip规则, 是否继续?`,
        "温馨提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        }
      )
        .then(() => {
          addVipRule(this.insertData).then((res) => {
            this.insertDialogVisible = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "新增成功",
              });
              this.getVipRuleHandler();
            }
          });
        })
        .catch(() => {});
    },
    //取消修改vip规则按钮
    cancelAddRuleHanlder() {
      this.insertDialogVisible = false;
    },
    //打开新增弹窗
    openAddDialogHandler() {
      this.insertDialogVisible = true;
    },
    //切换平台
    handleStatusChange() {
      this.selectData.currentPage = 1;
    },
    //查询vip规则
    getVipRuleHandler() {
      //开启加载动画
      this.dataLoading=true;
      getVipRule(this.selectData)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.selectData.currentPage = res.data.pageNum
          //关闭加载动画
          this.dataLoading=false;
        })
        .catch(() => {});
    },

    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.getVipRuleHandler();
    },
    handleCurrentChange(val) {
      this.selectData.currentPage = val;
      this.getVipRuleHandler();
    },
  },
  mounted() {
    this.getVipRuleHandler();
  },
};
</script>

<style>
</style>